package org.example;
import org.apache.poi.xwpf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student {
    String name;
    String gender;
    String studentId;
    List<Volunteer> volunteers = new ArrayList<>();

    public void addVolunteer(Volunteer volunteer) {
        if (volunteers.size() < 30) {
            volunteers.add(volunteer);
        } else {
            System.out.println("最多只能填报30个专业组！");
        }
    }

    public void changeVolunteerOrder(int index1, int index2) {
        if (index1 >= 0 && index1 < volunteers.size() && index2 >= 0 && index2 < volunteers.size()) {
            Volunteer temp = volunteers.get(index1);
            volunteers.set(index1, volunteers.get(index2));
            volunteers.set(index2, temp);
        } else {
            System.out.println("索引超出范围！");
        }
    }
}

class Volunteer {
    String university;
    List<String> majors = new ArrayList<>();

    public void addMajor(String major) {
        if (majors.size() < 6) {
            majors.add(major);
        } else {
            System.out.println("每个院校专业组最多只能有6个专业！");
        }
    }
}

public class VolunteerSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Student student = new Student();
        boolean valid = false;

        // 输入学生姓名
        while (!valid) {
            System.out.print("请输入姓名: ");
            student.name = scanner.nextLine();
            if (!student.name.isEmpty()) {
                valid = true;
            } else {
                System.out.println("姓名不能为空，请重新输入！");
            }
        }

        // 输入学生性别
        valid = false;
        while (!valid) {
            System.out.print("请输入性别 (男/女): ");
            student.gender = scanner.nextLine();
            if ("男".equals(student.gender) || "女".equals(student.gender)) {
                valid = true;
            } else {
                System.out.println("性别输入错误，请输入 男 或 女！");
            }
        }
        System.out.print("请输入学号: ");
        student.studentId = scanner.nextLine();

        // 输入志愿
        while (true) {
            System.out.print("是否继续添加志愿？(y/n): ");
            String choice = scanner.nextLine();
            if (choice.equalsIgnoreCase("n")) break;

            Volunteer volunteer = new Volunteer();
            System.out.print("请输入院校+专业组代号: ");
            volunteer.university = scanner.nextLine();

            while (true) {
                System.out.print("是否继续添加专业？(y/n): ");
                choice = scanner.nextLine();
                if (choice.equalsIgnoreCase("n")) break;

                System.out.print("请输入专业名称: ");
                String major = scanner.nextLine();
                volunteer.addMajor(major);
            }

            student.addVolunteer(volunteer);
        }

        // 改变志愿顺序
        System.out.print("是否改变志愿顺序？(y/n): ");
        String choice = scanner.nextLine();
        if (choice.equalsIgnoreCase("y")) {
            System.out.print("请输入第一个志愿的索引: ");
            int index1 = scanner.nextInt();
            System.out.print("请输入第二个志愿的索引: ");
            int index2 = scanner.nextInt();
            student.changeVolunteerOrder(index1, index2);
        }

        // 输出并打印志愿表
        printVolunteerForm(student);
    }

    public static void printVolunteerForm(Student student) {
        XWPFDocument document = new XWPFDocument();//创建word文件


        XWPFParagraph titleParagraph = document.createParagraph();
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText("高考志愿填报表");
        titleRun.setBold(true);
        titleRun.setFontSize(20);

        XWPFParagraph studentInfoParagraph = document.createParagraph();
        XWPFRun studentInfoRun = studentInfoParagraph.createRun();
        studentInfoRun.setText("姓名: " + student.name + "\t性别: " + student.gender + "\t学号: " + student.studentId);

        for (int i = 0; i < student.volunteers.size(); i++) {
            Volunteer volunteer = student.volunteers.get(i);
            XWPFParagraph volunteerParagraph = document.createParagraph();
            XWPFRun volunteerRun = volunteerParagraph.createRun();
            volunteerRun.setText("志愿" + (i + 1) + ": 院校志愿组 - " + volunteer.university);

            for (String major : volunteer.majors) {
                XWPFParagraph majorParagraph = document.createParagraph();
                XWPFRun majorRun = majorParagraph.createRun();
                majorRun.setText("\t专业: " + major);
            }
        }

        try (FileOutputStream out = new FileOutputStream("VolunteerForm.docx")) {
            document.write(out);
            System.out.println("志愿表已成功生成并保存为VolunteerForm.docx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}